package com.mjfeed.activity;

import java.util.ArrayList;
import java.util.List;

import com.mjfeed.adapter.FavoriteAdapter;
import com.mjfeed.bean.ProductJSON;
import com.mjfeed.bean.ProductListJSON;
import com.mjfeed.service.FavoriteService;
import com.mjfeed.util.NetUtils;
import com.mjfeed.util.SPUtils;
import com.mjfeed.widget.LoadingDialog;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 我的关注
 * @author zhaominglei
 * @date 2014-12-29
 * 
 */
public class FavoriteListActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = FavoriteListActivity.class.getSimpleName();
	
	private ImageView goHome;
	private LoadingDialog loadingDialog; // 加载框
	private TextView emptyHint;
	private ListView favoriteItemView;
	private FavoriteAdapter favoriteAdapter;
	private List<ProductJSON> products = new ArrayList<ProductJSON>();
	private String referer; // 来自那个链接
	private String username; //用户名
	private String sign; //签名
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_favorite_list);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.favorite_gohome);
		emptyHint = (TextView) findViewById(R.id.favorite_empty_hint);
		favoriteItemView = (ListView) findViewById(R.id.favorite_item_listview);

		goHome.setOnClickListener(this);
		favoriteAdapter = new FavoriteAdapter(this, products);
		favoriteItemView.setAdapter(favoriteAdapter);
		
		emptyHint.setVisibility(View.INVISIBLE);
		referer = getIntent().getStringExtra("referer");
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		
		getFavorites();
	}

	private void getFavorites() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog = new LoadingDialog(this);
			loadingDialog.show();
			new FavoriteListAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.favorite_gohome:
			finish();
			break;

		default:
			break;
		}
	}
	
	public class FavoriteListAsyncTask extends AsyncTask<String, String, ProductListJSON> {

		@Override
		protected ProductListJSON doInBackground(String... params) {
			return new FavoriteService().getFavorite(referer, username, sign);
		}

		@Override
		protected void onPostExecute(ProductListJSON result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				emptyHint.setVisibility(View.INVISIBLE);
				favoriteAdapter.setProducts(result.getRecordList());
				favoriteAdapter.notifyDataSetChanged();
			} else {
				emptyHint.setVisibility(View.VISIBLE);
			}
		}
	}
}
